//获取定位
function getLocation(){
	let that = this
	uni.getLocation({
		  type: 'gcj02',
		  success: function(res) {
			 //缓存经纬度
			 uni.setStorageSync('lat',res.latitude)
			 uni.setStorageSync('lng',res.longitude)
			 
			 //获取城市
			 loadCity(res.latitude,res.longitude)
		  },
		  fail: function(err) {
			 console.log(err)
		  }
	});
}					

//根据经纬度获取城市名称
function loadCity(lat,lng) {
	let that = this
	let key = 'WBKBZ-ZRRW2-BLGU3-C2P2I-HNPVT-SHF5W';
	uni.request({
		url: 'https://apis.map.qq.com/ws/geocoder/v1/?location=' + lat + ',' + lng + '&key=' + key,
		method: "GET",
		success(res) {
			let data = res.data; //获取到所有定位的数据
			let cityName = res.data.result.address_component.city
			//去掉市
			if(cityName[cityName.length-1] == '市'){
				cityName = cityName.slice(0, -1)
			}
			uni.setStorageSync('city',cityName)
		},
		fail(err) {
			console.log(err)
		}
	})	
}		
							
module.exports = {
	getLocation: getLocation,
	loadCity: loadCity
}